home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / StreamTokenizer.java < prev    next >
Text File  |  1998-09-22  |  25KB  |  749 lines

  1. /*
  2.  * @(#)StreamTokenizer.java    1.21 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * The <code>StreamTokenizer</code> class takes an input stream and 
  20.  * parses it into "tokens", allowing the tokens to be 
  21.  * read one at a time. The parsing process is controlled by a table 
  22.  * and a number of flags that can be set to various states. The 
  23.  * stream tokenizer can recognize identifiers, numbers, quoted 
  24.  * strings, and various comment styles. 
  25.  * <p>
  26.  * Each byte read from the input stream is regarded as a character 
  27.  * in the range <code>'\u0000'</code> through <code>'\u00FF'</code>. 
  28.  * The character value is used to look up five possible attributes of 
  29.  * the character: <i>white space</i>, <i>alphabetic</i>, 
  30.  * <i>numeric</i>, <i>string quote</i>, and <i>comment character</i>. 
  31.  * Each character can have zero or more of these attributes. 
  32.  * <p>
  33.  * In addition, an instance has four flags. These flags indicate: 
  34.  * <ul>
  35.  * <li>Whether line terminators are to be returned as tokens or treated 
  36.  *     as white space that merely separates tokens. 
  37.  * <li>Whether C-style comments are to be recognized and skipped. 
  38.  * <li>Whether C++-style comments are to be recognized and skipped. 
  39.  * <li>Whether the characters of identifiers are converted to lowercase. 
  40.  * </ul>
  41.  * <p>
  42.  * A typical application first constructs an instance of this class, 
  43.  * sets up the syntax tables, and then repeatedly loops calling the 
  44.  * <code>nextToken</code> method in each iteration of the loop until 
  45.  * it returns the value <code>TT_EOF</code>. 
  46.  *
  47.  * @author  James Gosling
  48.  * @version 1.21, 07/01/98
  49.  * @see     java.io.StreamTokenizer#nextToken()
  50.  * @see     java.io.StreamTokenizer#TT_EOF
  51.  * @since   JDK1.0
  52.  */
  53.  
  54. public class StreamTokenizer {
  55.  
  56.     /* Only one of these will be non-null */
  57.     private Reader reader = null;
  58.     private InputStream input = null;
  59.  
  60.     private char buf[] = new char[20];
  61.     private int peekc;
  62.     private boolean pushedBack;
  63.     private boolean forceLower;
  64.     /** The line number of the last token read */
  65.     private int LINENO = 1;
  66.  
  67.     private boolean eolIsSignificantP = false;
  68.     private boolean slashSlashCommentsP = false;
  69.     private boolean slashStarCommentsP = false;
  70.  
  71.     private byte ctype[] = new byte[256];
  72.     private static final byte CT_WHITESPACE = 1;
  73.     private static final byte CT_DIGIT = 2;
  74.     private static final byte CT_ALPHA = 4;
  75.     private static final byte CT_QUOTE = 8;
  76.     private static final byte CT_COMMENT = 16;
  77.  
  78.     /** 
  79.      * After a call to the <code>nextToken</code> method, this field 
  80.      * contains the type of the token just read. For a single character 
  81.      * token, its value is the single character, converted to an integer. 
  82.      * For a quoted string token (see , its value is the quote character. 
  83.      * Otherwise, its value is one of the following: 
  84.      * <ul>
  85.      * <li><code>TT_WORD</code> indicates that the token is a word.
  86.      * <li><code>TT_NUMBER</code> indicates that the token is a number.
  87.      * <li><code>TT_EOL</code> indicates that the end of line has been read. 
  88.      *     The field can only have this value if the 
  89.      *     <code>eolIsSignificant</code> method has been called with the 
  90.      *     argument <code>true</code>. 
  91.      * <li><code>TT_EOF</code> indicates that the end of the input stream 
  92.      *     has been reached. 
  93.      * </ul>
  94.      *
  95.      * @see     java.io.StreamTokenizer#eolIsSignificant(boolean)
  96.      * @see     java.io.StreamTokenizer#nextToken()
  97.      * @see     java.io.StreamTokenizer#quoteChar(int)
  98.      * @see     java.io.StreamTokenizer#TT_EOF
  99.      * @see     java.io.StreamTokenizer#TT_EOL
  100.      * @see     java.io.StreamTokenizer#TT_NUMBER
  101.      * @see     java.io.StreamTokenizer#TT_WORD
  102.      */
  103.     public int ttype = TT_NOTHING;
  104.  
  105.     /** 
  106.      * A constant indicating that the end of the stream has been read. 
  107.      */
  108.     public static final int TT_EOF = -1;
  109.  
  110.     /** 
  111.      * A constant indicating that the end of the line has been read. 
  112.      */
  113.     public static final int TT_EOL = '\n';
  114.  
  115.     /** 
  116.      * A constant indicating that a number token has been read. 
  117.      */
  118.     public static final int TT_NUMBER = -2;
  119.  
  120.     /** 
  121.      * A constant indicating that a word token has been read. 
  122.      */
  123.     public static final int TT_WORD = -3;
  124.  
  125.     /* A constant indicating that no token has been read, used for
  126.      * initializing ttype.  FIXME This could be made public and
  127.      * made available as the part of the API in a future release.
  128.      */
  129.     private static final int TT_NOTHING = -4;
  130.     
  131.     /**
  132.      * If the current token is a word token, this field contains a 
  133.      * string giving the characters of the word token. When the current 
  134.      * token is a quoted string token, this field contains the body of 
  135.      * the string. 
  136.      * <p>
  137.      * The current token is a word when the value of the 
  138.      * <code>ttype</code> field is <code>TT_WORD</code>. The current token is
  139.      * a quoted string token when the value of the <code>ttype</code> field is
  140.      * a quote character.
  141.      *
  142.      * @see     java.io.StreamTokenizer#quoteChar(int)
  143.      * @see     java.io.StreamTokenizer#TT_WORD
  144.      * @see     java.io.StreamTokenizer#ttype
  145.      * @since JDK1.0
  146.      */
  147.     public String sval;
  148.  
  149.     /**
  150.      * If the current token is a number, this field contains the value 
  151.      * of that number. The current token is a number when the value of 
  152.      * the <code>ttype</code> field is <code>TT_NUMBER</code>. 
  153.      *
  154.      * @see     java.io.StreamTokenizer#TT_NUMBER
  155.      * @see     java.io.StreamTokenizer#ttype
  156.      */
  157.     public double nval;
  158.  
  159.     /** Private constructor that initializes everything except the streams. */
  160.     private StreamTokenizer() {
  161.     wordChars('a', 'z');
  162.     wordChars('A', 'Z');
  163.     wordChars(128 + 32, 255);
  164.     whitespaceChars(0, ' ');
  165.     commentChar('/');
  166.     quoteChar('"');
  167.     quoteChar('\'');
  168.     parseNumbers();
  169.     }
  170.  
  171.     /**
  172.      * Creates a stream tokenizer that parses the specified input 
  173.      * stream. The stream tokenizer is initialized to the following 
  174.      * default state: 
  175.      * <ul>
  176.      * <li>All byte values <code>'A'</code> through <code>'Z'</code>, 
  177.      *     <code>'a'</code> through <code>'z'</code>, and 
  178.      *     <code>'\u00A0'</code> through <code>'\u00FF'</code> are
  179.      *     considered to be alphabetic. 
  180.      * <li>All byte values <code>'\u0000'</code> through 
  181.      *     <code>'\u0020'</code> are considered to be white space. 
  182.      * <li><code>'/'</code> is a comment character. 
  183.      * <li>Single quote <code>'\''</code> and double quote <code>'"'</code> 
  184.      *     are string quote characters. 
  185.      * <li>Numbers are parsed. 
  186.      * <li>Ends of lines are treated as white space, not as separate tokens. 
  187.      * <li>C-style and C++-style comments are not recognized. 
  188.      * </ul>
  189.      *
  190.      * @deprecated As of JDK version 1.1, the preferred way to tokenize an
  191.      * input stream is to convert it into a character stream, for example:
  192.      * <p>
  193.      * <pre>
  194.      *   Reader r = new BufferedReader(new InputStreamReader(is));
  195.      *   StreamTokenizer st = new StreamTokenizer(r);
  196.      * </pre>
  197.      *
  198.      * @param      is        an input stream.
  199.      * @see        java.io.BufferedReader
  200.      * @see        java.io.InputStreamReader
  201.      * @see        java.io.StreamTokenizer#StreamTokenizer(java.io.Reader)
  202.      */
  203.     public StreamTokenizer(InputStream is) {
  204.     this();
  205.     input = is;
  206.     }
  207.  
  208.     /**
  209.      * Create a tokenizer that parses the given character stream.
  210.      * @since   JDK1.1
  211.      */
  212.     public StreamTokenizer(Reader r) {
  213.     this();
  214.     reader = r;
  215.     }
  216.  
  217.     /** 
  218.      * Resets this tokenizer's syntax table so that all characters are 
  219.      * "ordinary." See the <code>ordinaryChar</code> method 
  220.      * for more information on a character being ordinary. 
  221.      *
  222.      * @see     java.io.StreamTokenizer#ordinaryChar(int)
  223.      */
  224.     public void resetSyntax() {
  225.     for (int i = ctype.length; --i >= 0;)
  226.         ctype[i] = 0;
  227.     }
  228.  
  229.     /** 
  230.      * Specifies that all characters <i>c</i> in the range 
  231.      * <code>low <= <i>c</i> <= high</code> 
  232.      * are word constituents. A word token consists of a word constituent 
  233.      * followed by zero or more word constituents or number constituents. 
  234.      *
  235.      * @param   low   the low end of the range.
  236.      * @param   hi    the high end of the range.
  237.      */
  238.     public void wordChars(int low, int hi) {
  239.     if (low < 0)
  240.         low = 0;
  241.     if (hi >= ctype.length)
  242.         hi = ctype.length - 1;  
  243.     while (low <= hi)
  244.         ctype[low++] |= CT_ALPHA;
  245.     }
  246.  
  247.     /** 
  248.      * Specifies that all characters <i>c</i> in the range 
  249.      * <code>low <= <i>c</i> <= high</code> 
  250.      * are white space characters. White space characters serve only to 
  251.      * separate tokens in the input stream. 
  252.      *
  253.      * @param   low   the low end of the range.
  254.      * @param   hi    the high end of the range.
  255.      */
  256.     public void whitespaceChars(int low, int hi) {
  257.     if (low < 0)
  258.         low = 0;
  259.     if (hi >= ctype.length)
  260.         hi = ctype.length - 1;
  261.     while (low <= hi)
  262.         ctype[low++] = CT_WHITESPACE;
  263.     }
  264.  
  265.     /** 
  266.      * Specifies that all characters <i>c</i> in the range 
  267.      * <code>low <= <i>c</i> <= high</code> 
  268.      * are "ordinary" in this tokenizer. See the 
  269.      * <code>ordinaryChar</code> method for more information on a 
  270.      * character being ordinary. 
  271.      *
  272.      * @param   low   the low end of the range.
  273.      * @param   hi    the high end of the range.
  274.      * @see     java.io.StreamTokenizer#ordinaryChar(int)
  275.      */
  276.     public void ordinaryChars(int low, int hi) {
  277.     if (low < 0)
  278.         low = 0;
  279.     if (hi >= ctype.length)
  280.         hi = ctype.length - 1;
  281.     while (low <= hi)
  282.         ctype[low++] = 0;
  283.     }
  284.  
  285.     /** 
  286.      * Specifies that the character argument is "ordinary" 
  287.      * in this tokenizer. It removes any special significance the 
  288.      * character has as a comment character, word component, string 
  289.      * delimiter, white space, or number character. When such a character 
  290.      * is encountered by the parser, the parser treates it as a
  291.      * single-character token and sets <code>ttype</code> field to the
  292.      * character value. 
  293.      *
  294.      * @param   ch   the character.
  295.      * @see     java.io.StreamTokenizer#ttype
  296.      */
  297.     public void ordinaryChar(int ch) {
  298.         if (ch >= 0 && ch < ctype.length)
  299.           ctype[ch] = 0;
  300.     }
  301.  
  302.     /** 
  303.      * Specified that the character argument starts a single-line 
  304.      * comment. All characters from the comment character to the end of 
  305.      * the line are ignored by this stream tokenizer. 
  306.      *
  307.      * @param   ch   the character.
  308.      */
  309.     public void commentChar(int ch) {
  310.         if (ch >= 0 && ch < ctype.length)
  311.         ctype[ch] = CT_COMMENT;
  312.     }
  313.  
  314.     /** 
  315.      * Specifies that matching pairs of this character delimit string 
  316.      * constants in this tokenizer. 
  317.      * <p>
  318.      * When the <code>nextToken</code> method encounters a string 
  319.      * constant, the <code>ttype</code> field is set to the string 
  320.      * delimiter and the <code>sval</code> field is set to the body of 
  321.      * the string. 
  322.      * <p>
  323.      * If a string quote character is encountered, then a string is 
  324.      * recognized, consisting of all characters after (but not including) 
  325.      * the string quote character, up to (but not including) the next 
  326.      * occurrence of that same string quote character, or a line 
  327.      * terminator, or end of file. The usual escape sequences such as 
  328.      * <code>"\n"</code> and <code>"\t"</code> are recognized and 
  329.      * converted to single characters as the string is parsed. 
  330.      *
  331.      * @param   ch   the character.
  332.      * @see     java.io.StreamTokenizer#nextToken()
  333.      * @see     java.io.StreamTokenizer#sval
  334.      * @see     java.io.StreamTokenizer#ttype
  335.      */
  336.     public void quoteChar(int ch) {
  337.         if (ch >= 0 && ch < ctype.length)
  338.          ctype[ch] = CT_QUOTE;
  339.     }
  340.  
  341.     /** 
  342.      * Specifies that numbers should be parsed by this tokenizer. The 
  343.      * syntax table of this tokenizer is modified so that each of the twelve
  344.      * characters:
  345.      * <ul><code>
  346.      *      0 1 2 3 4 5 6 7 8 9 . -
  347.      * </code></ul>
  348.      * <p>
  349.      * has the "numeric" attribute. 
  350.      * <p>
  351.      * When the parser encounters a word token that has the format of a 
  352.      * double precision floating-point number, it treats the token as a 
  353.      * number rather than a word, by setting the the <code>ttype</code> 
  354.      * field to the value <code>TT_NUMBER</code> and putting the numeric 
  355.      * value of the token into the <code>nval</code> field. 
  356.      *
  357.      * @see     java.io.StreamTokenizer#nval
  358.      * @see     java.io.StreamTokenizer#TT_NUMBER
  359.      * @see     java.io.StreamTokenizer#ttype
  360.      */
  361.     public void parseNumbers() {
  362.     for (int i = '0'; i <= '9'; i++)
  363.         ctype[i] |= CT_DIGIT;
  364.     ctype['.'] |= CT_DIGIT;
  365.     ctype['-'] |= CT_DIGIT;
  366.     }
  367.  
  368.     /**
  369.      * Determines whether or not ends of line are treated as tokens.
  370.      * If the flag argument is true, this tokenizer treats end of lines 
  371.      * as tokens; the <code>nextToken</code> method returns 
  372.      * <code>TT_EOL</code> and also sets the <code>ttype</code> field to 
  373.      * this value when an end of line is read. 
  374.      * <p>
  375.      * A line is a sequence of characters ending with either a 
  376.      * carriage-return character (<code>'\r'</code>) or a newline 
  377.      * character (<code>'\n'</code>). In addition, a carriage-return 
  378.      * character followed immediately by a newline character is treated 
  379.      * as a single end-of-line token. 
  380.      * <p>
  381.      * If the <code>flag</code> is false, end-of-line characters are 
  382.      * treated as white space and serve only to separate tokens. 
  383.      *
  384.      * @param   flag   <code>true</code> indicates that end-of-line characters
  385.      *                 are separate tokens; <code>false</code> indicates that
  386.      *                 end-of-line characters are white space.
  387.      * @see     java.io.StreamTokenizer#nextToken()
  388.      * @see     java.io.StreamTokenizer#ttype
  389.      * @see     java.io.StreamTokenizer#TT_EOL
  390.      */
  391.     public void eolIsSignificant(boolean flag) {
  392.     eolIsSignificantP = flag;
  393.     }
  394.  
  395.     /** 
  396.      * Determines whether or not the tokenizer recognizes C-style comments.
  397.      * If the flag argument is <code>true</code>, this stream tokenizer 
  398.      * recognizes C-style comments. All text between successive 
  399.      * occurrences of <code>/*</code> and <code>*/</code> are discarded. 
  400.      * <p>
  401.      * If the flag argument is <code>false</code>, then C-style comments 
  402.      * are not treated specially. 
  403.      *
  404.      * @param   flag   <code>true</code> indicates to recognize and ignore
  405.      *                 C-style comments.
  406.      */
  407.     public void slashStarComments(boolean flag) {
  408.     slashStarCommentsP = flag;
  409.     }
  410.  
  411.     /** 
  412.      * Determines whether or not the tokenizer recognizes C++-style comments.
  413.      * If the flag argument is <code>true</code>, this stream tokenizer 
  414.      * recognizes C++-style comments. Any occurrence of two consecutive 
  415.      * slash characters (<code>'/'</code>) is treated as the beginning of 
  416.      * a comment that extends to the end of the line. 
  417.      * <p>
  418.      * If the flag argument is <code>false</code>, then C++-style 
  419.      * comments are not treated specially. 
  420.      *
  421.      * @param   flag   <code>true</code> indicates to recognize and ignore
  422.      *                 C++-style comments.
  423.      */
  424.     public void slashSlashComments(boolean flag) {
  425.     slashSlashCommentsP = flag;
  426.     }
  427.  
  428.     /**
  429.      * Determines whether or not word token are automatically lowercased.
  430.      * If the flag argument is <code>true</code>, then the value in the 
  431.      * <code>sval</code> field is lowercased whenever a word token is 
  432.      * returned (the <code>ttype</code> field has the 
  433.      * value <code>TT_WORD</code> by the <code>nextToken</code> method 
  434.      * of this tokenizer. 
  435.      * <p>
  436.      * If the flag argument is <code>false</code>, then the 
  437.      * <code>sval</code> field is not modified. 
  438.      *
  439.      * @param   fl   <code>true</code> indicates that all word tokens should
  440.      *               be lowercased.
  441.      * @see     java.io.StreamTokenizer#nextToken()
  442.      * @see     java.io.StreamTokenizer#ttype
  443.      * @see     java.io.StreamTokenizer#TT_WORD
  444.      */
  445.     public void lowerCaseMode(boolean fl) {
  446.     forceLower = fl;
  447.     }
  448.  
  449.     /** Read the next character */
  450.     private int read() throws IOException {
  451.     if (reader != null)
  452.         return reader.read();
  453.     else if (input != null)
  454.         return input.read();
  455.     else
  456.         throw new IllegalStateException();
  457.     }
  458.  
  459.     /** 
  460.      * Parses the next token from the input stream of this tokenizer. 
  461.      * The type of the next token is returned in the <code>ttype</code> 
  462.      * field. Additional information about the token may be in the 
  463.      * <code>nval</code> field or the <code>sval</code> field of this 
  464.      * tokenizer. 
  465.      * <p>
  466.      * Typical clients of this
  467.      * class first set up the syntax tables and then sit in a loop
  468.      * calling nextToken to parse successive tokens until TT_EOF
  469.      * is returned. 
  470.      *
  471.      * @return     the value of the <code>ttype</code> field.
  472.      * @exception  IOException  if an I/O error occurs.
  473.      * @see        java.io.StreamTokenizer#nval
  474.      * @see        java.io.StreamTokenizer#sval
  475.      * @see        java.io.StreamTokenizer#ttype
  476.      */
  477.     public int nextToken() throws IOException {
  478.     if (pushedBack) {
  479.         pushedBack = false;
  480.         return ttype;
  481.     }
  482.     byte ct[] = ctype;
  483.     int c; 
  484.     sval = null;
  485.  
  486.     if (ttype == TT_NOTHING) {
  487.         c = read();
  488.         if (c >= 0)    // ttype is surely overwritten below to its correct value.
  489.             ttype = c; // for now we just make sure it isn't TT_NOTHING
  490.     } else {
  491.         c = peekc;
  492.     }
  493.     
  494.     if (c < 0)
  495.         return ttype = TT_EOF;
  496.     int ctype = c < 256 ? ct[c] : CT_ALPHA;
  497.     while ((ctype & CT_WHITESPACE) != 0) {
  498.         if (c == '\r') {
  499.         LINENO++;
  500.         c = read();
  501.         if (c == '\n')
  502.             c = read();
  503.         if (eolIsSignificantP) {
  504.             peekc = c;
  505.             return ttype = TT_EOL;
  506.         }
  507.         } else {
  508.         if (c == '\n') {
  509.             LINENO++;
  510.             if (eolIsSignificantP) {
  511.             peekc = read();
  512.             return ttype = TT_EOL;
  513.             }
  514.         }
  515.         c = read();
  516.         }
  517.         if (c < 0)
  518.         return ttype = TT_EOF;
  519.         ctype = c < 256 ? ct[c] : CT_ALPHA;
  520.     }
  521.     if ((ctype & CT_DIGIT) != 0) {
  522.         boolean neg = false;
  523.         if (c == '-') {
  524.         c = read();
  525.         if (c != '.' && (c < '0' || c > '9')) {
  526.             peekc = c;
  527.             return ttype = '-';
  528.         }
  529.         neg = true;
  530.         }
  531.         double v = 0;
  532.         int decexp = 0;
  533.         int seendot = 0;
  534.         while (true) {
  535.         if (c == '.' && seendot == 0)
  536.             seendot = 1;
  537.         else if ('0' <= c && c <= '9') {
  538.             v = v * 10 + (c - '0');
  539.             decexp += seendot;
  540.         } else
  541.             break;
  542.         c = read();
  543.         }
  544.         peekc = c;
  545.         if (decexp != 0) {
  546.         double denom = 10;
  547.         decexp--;
  548.         while (decexp > 0) {
  549.             denom *= 10;
  550.             decexp--;
  551.         }
  552.         /* do one division of a likely-to-be-more-accurate number */
  553.         v = v / denom;
  554.         }
  555.         nval = neg ? -v : v;
  556.         return ttype = TT_NUMBER;
  557.     }
  558.     if ((ctype & CT_ALPHA) != 0) {
  559.         int i = 0;
  560.         do {
  561.         if (i >= buf.length) {
  562.             char nb[] = new char[buf.length * 2];
  563.             System.arraycopy(buf, 0, nb, 0, buf.length);
  564.             buf = nb;
  565.         }
  566.         buf[i++] = (char) c;
  567.         c = read();
  568.         ctype = c < 0 ? CT_WHITESPACE : c < 256 ? ct[c] : CT_ALPHA;
  569.         } while ((ctype & (CT_ALPHA | CT_DIGIT)) != 0);
  570.         peekc = c;
  571.         sval = String.copyValueOf(buf, 0, i);
  572.         if (forceLower)
  573.         sval = sval.toLowerCase();
  574.         return ttype = TT_WORD;
  575.     }
  576.     if ((ctype & CT_COMMENT) != 0) {
  577.         while ((c = read()) != '\n' && c != '\r' && c >= 0);
  578.         peekc = c;
  579.         return nextToken();
  580.     }
  581.     if ((ctype & CT_QUOTE) != 0) {
  582.         ttype = c;
  583.         int i = 0;
  584.         // invariants (because \Octal needs a lookahead):
  585.         //      (i)  c contains char value 
  586.         //      (ii) peekc contains the lookahead
  587.         peekc = read(); 
  588.         while (peekc >= 0 && peekc != ttype && peekc != '\n' && peekc != '\r') {
  589.             if (peekc == '\\') {
  590.                c = read();
  591.             int first = c;   // to allow \377, but not \477
  592.             if (c >= '0' && c <= '7') {
  593.             c = c - '0';
  594.             int c2 = read();
  595.             if ('0' <= c2 && c2 <= '7') {
  596.                 c = (c << 3) + (c2 - '0');
  597.                 c2 = read();
  598.                 if ('0' <= c2 && c2 <= '7' && first <= '3') {
  599.                 c = (c << 3) + (c2 - '0');
  600.                 peekc = read();
  601.                 } else
  602.                 peekc = c2;
  603.             } else
  604.               peekc = c2;
  605.             } else {
  606.                   switch (c) {
  607.             case 'a':
  608.                 c = 0x7;
  609.                 break;
  610.             case 'b':
  611.                 c = '\b';
  612.                 break;
  613.             case 'f':
  614.                 c = 0xC;
  615.                 break;
  616.             case 'n':
  617.                 c = '\n';
  618.                 break;
  619.                 case 'r':
  620.                 c = '\r';
  621.                 break;
  622.             case 't':
  623.                 c = '\t';
  624.                 break;
  625.             case 'v':
  626.                 c = 0xB;
  627.                 break;
  628.             }
  629.             peekc = read();
  630.             }
  631.         } else {
  632.             c = peekc;
  633.             peekc = read();
  634.         }
  635.         
  636.         if (i >= buf.length) {
  637.             char nb[] = new char[buf.length * 2];
  638.             System.arraycopy(buf, 0, nb, 0, buf.length);
  639.             buf = nb;
  640.         }
  641.         buf[i++] = (char) c;
  642.         }
  643.         if (peekc == ttype)  // keep \n or \r intact in peekc
  644.             peekc = read();
  645.         sval = String.copyValueOf(buf, 0, i);
  646.         return ttype;
  647.     }
  648.     if (c == '/' && (slashSlashCommentsP || slashStarCommentsP)) {
  649.         c = read();
  650.         if (c == '*' && slashStarCommentsP) {
  651.         int prevc = 0;
  652.         while ((c = read()) != '/' || prevc != '*') {
  653.             if (c == '\r') {
  654.             LINENO++;
  655.             c = read();
  656.             if (c == '\n') {
  657.                 c = read();
  658.             }
  659.             } else {
  660.                 if (c == '\n') {
  661.                 LINENO++;
  662.                 c = read();
  663.             }
  664.             }
  665.             if (c < 0)
  666.                 return ttype = TT_EOF;
  667.             prevc = c;
  668.         }
  669.         peekc = read();
  670.         return nextToken();
  671.         } else if (c == '/' && slashSlashCommentsP) {
  672.             while ((c = read()) != '\n' && c != '\r' && c >= 0);
  673.             peekc = c;
  674.         return nextToken();
  675.         } else {
  676.         peekc = c;
  677.         return ttype = '/';
  678.         }
  679.     }
  680.     peekc = read();
  681.     return ttype = c;
  682.     }
  683.  
  684.     /**
  685.      * Causes the next call to the <code>nextToken</code> method of this 
  686.      * tokenizer to return the current value in the <code>ttype</code> 
  687.      * field, and not to modify the value in the <code>nval</code> or 
  688.      * <code>sval</code> field. 
  689.      *
  690.      * @see     java.io.StreamTokenizer#nextToken()
  691.      * @see     java.io.StreamTokenizer#nval
  692.      * @see     java.io.StreamTokenizer#sval
  693.      * @see     java.io.StreamTokenizer#ttype
  694.      */
  695.     public void pushBack() {
  696.         if (ttype != TT_NOTHING)   // no-op if nextToken() not called
  697.         pushedBack = true;
  698.     }
  699.  
  700.     /**
  701.      * Return the current line number.
  702.      *
  703.      * @return  the current line number of this stream tokenizer.
  704.      */
  705.     public int lineno() {
  706.     return LINENO;
  707.     }
  708.  
  709.     /**
  710.      * Returns the string representation of the current stream token. 
  711.      *
  712.      * @return  a string representation of the token specified by the
  713.      *          <code>ttype</code>, <code>nval</code>, and <code>sval</code>
  714.      *          fields.
  715.      * @see     java.io.StreamTokenizer#nval
  716.      * @see     java.io.StreamTokenizer#sval
  717.      * @see     java.io.StreamTokenizer#ttype
  718.      */
  719.     public String toString() {
  720.     String ret;
  721.     switch (ttype) {
  722.       case TT_EOF:
  723.         ret = "EOF";
  724.         break;
  725.       case TT_EOL:
  726.         ret = "EOL";
  727.         break;
  728.       case TT_WORD:
  729.         ret = sval;
  730.         break;
  731.       case TT_NUMBER:
  732.         ret = "n=" + nval;
  733.         break;
  734.          case TT_NOTHING:  
  735.         ret = "NOTHING";
  736.         break;
  737.       default:{
  738.         char s[] = new char[3];
  739.         s[0] = s[2] = '\'';
  740.         s[1] = (char) ttype;
  741.         ret = new String(s);
  742.         break;
  743.         }
  744.     }
  745.     return "Token[" + ret + "], line " + LINENO;
  746.     }
  747.  
  748. }
  749.